package com.gp.service.impl;

import com.gp.dao.*;
import com.gp.service.CarService;
import com.gp.vo.CarVo;
import com.gp.vo.TbCarVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gp.entity.*;
import com.gp.util.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("carService")
public class CarServiceImpl implements CarService {

    @Autowired(required = false)
    private TbCarDao tbCarDao;
    @Autowired(required = false)
    private TbCarTypeDao tbCarTypeDao;
    @Autowired(required = false)
    private TbBrandDao tbBrandDao;
    @Autowired(required = false)
    private TbCarTypeConfugDetaDao tbCarTypeConfugDetaDao;
    @Autowired(required = false)
    private TbShopDao tbShopDao;
    @Autowired(required = false)
    private MaintainShopDao maintainShopDao;
    @Autowired(required = false)
    private MaintainRecordDao maintainRecordDao;

    @Override
    public CommonResult findAllCar(Integer page, Integer limit, TbCarVo tbCarVo) {
        PageHelper.startPage(page, limit);
        List<TbCar> allCar = tbCarDao.findAllCar(tbCarVo.getCname(), tbCarVo.getCp());
        PageInfo<TbCar> pageInfo = new PageInfo<>(allCar);
        return new CommonResult(200, "查询成功", pageInfo);
    }

    @Override
    public CommonResult delCarById(Integer id) {
        int i = tbCarDao.deleteById(id);
        if (i > 0) {
            return new CommonResult(200, "删除成功", null);
        } else {
            return new CommonResult(500, "删除失败", null);
        }
    }

    @Override
    public CommonResult carDetails() {
        List<TbBrand> tbBrands = tbBrandDao.selectList(null);
        List<TbCarType> tbCarTypes = tbCarTypeDao.selectList(null);
        List<TbCarTypeConfigDetails> tbCarTypeConfigDetails = tbCarTypeConfugDetaDao.selectList(null);
        List<TbShop> tbShops = tbShopDao.selectList(null);
        List<MaintainShop> maintainShops = maintainShopDao.selectList(null);
        Map<String, Object> map = new HashMap<>();
        map.put("tbBrand", tbBrands);
        map.put("tbTypes", tbCarTypes);
        map.put("deTails", tbCarTypeConfigDetails);
        map.put("tbShops",tbShops);
        map.put("maintain",maintainShops);
        return new CommonResult(200, "ok成功", map);
    }

    @Override
    public CommonResult insert(CarVo carVo) {
       TbCar tbCar=new TbCar();
       tbCar.setCarname(carVo.getCarname());
       tbCar.setBrandId(carVo.getBrandId());
       tbCar.setConfigId(carVo.getConfigId());
       tbCar.setIntro(carVo.getIntro());
       tbCar.setVin(UUID.randomUUID()+"");
       tbCar.setBuyPrice(carVo.getBuyPrice());
       tbCar.setShopId(carVo.getShopId());
       tbCar.setType(carVo.getType());
       tbCar.setState(1);
       tbCar.setPrice(carVo.getPrice());
        int i = tbCarDao.insert(tbCar);
        return new CommonResult(200, "ok成功", null);
    }

    @Override
    public CommonResult update(TbCar tbCar) {
        int i = tbCarDao.updateById(tbCar);

        if (i > 0) {
            return new CommonResult(200, "修改成功", null);
        } else {
            return new CommonResult(500, "修改失败", null);
        }
    }

    @Override
    public CommonResult insertwei(MaintainRecord maintainRecord) {
        TbCar tbCar = tbCarDao.selectById(maintainRecord.getCarId());
        tbCar.setState(2);
        tbCarDao.updateById(tbCar);
        maintainRecord.setAddtime(new Date());
        //TODO  后续获取操作员姓名
        maintainRecord.setRemark("aaa");
        int i= maintainRecordDao.insert(maintainRecord);
        if (i > 0) {
            return new CommonResult(200, "插入成功", null);
        } else {
            return new CommonResult(500, "插入失败", null);
        }
    }


}
